<%@ page session="true" buffer="16kb" import="java.sql.*,java.util.*,java.text.*"%>
<%@ page contentType="text/html; charset=utf-8" %>
<%
response.setDateHeader("Expires", 0); // date in the past
response.addHeader("Cache-Control", "no-store, no-cache, must-revalidate"); // HTTP/1.1 
response.addHeader("Cache-Control", "post-check=0, pre-check=0"); 
response.addHeader("Pragma", "no-cache"); // HTTP/1.0 
%>
<% Locale locale = Locale.getDefault();
locale = new Locale("zh","CN"); 
response.setLocale(locale);%>
<% session.setMaxInactiveInterval(30*60); %>
<% 
String login = (String) session.getAttribute("project1_status");
if (login == null || !login.equals("login")) {
response.sendRedirect("login.jsp");
response.flushBuffer(); 
return; 
}%>
<%int ewAllowAdmin = 16; 
int ewCurSec = 31;%>
<% String userid = (String) session.getAttribute("project1_status_UserID"); 
Integer userlevel = (Integer) session.getAttribute("project1_status_UserLevel"); 
if (userid == null && userlevel != null && (userlevel.intValue() != -1) ) {	response.sendRedirect("login.jsp");
	response.flushBuffer(); 
	return; 
}%>
<%@ include file="db.jsp" %>
<%@ include file="jspmkrfn.jsp" %>
<%
String tmpfld = null;
String escapeString = "\\\\'";
request.setCharacterEncoding("utf-8");

// Get action
String a = request.getParameter("a");
String key = "";
if (a == null || a.length() == 0) {
	key = request.getParameter("key");
	if (key != null && key.length() > 0) {
		a = "C"; // Copy record
	} else {
		a = "I"; // Display blank record
	}
}
Object x_ID = null;
Object x_PWD = null;
Object x_NICK = null;

// Open Connection to the database
try{
	Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
	ResultSet rs = null;
	if (a.equals("C")){ // Get a record to display
		String tkey = "'" + key.replaceAll("'",escapeString) + "'";
		String strsql = "SELECT * FROM `account_info` WHERE `ID`=" + tkey;
		Integer userLevel = (Integer) session.getAttribute("project1_status_UserLevel");
    	if (userLevel != null && userLevel.intValue() != -1) { // Non system admin
			rs.updateString("NICK", (String) session.getAttribute("project1_status_UserID"));
		}
		rs = stmt.executeQuery(strsql);
		if (!rs.next()){
			rs.close();
			rs = null;
			stmt.close();
			stmt = null;
			conn.close();
			conn = null;
			out.clear();
			response.sendRedirect("account_infolist.jsp");
			response.flushBuffer();
			return;
		}
		rs.first();

			// Get the field contents
	if (rs.getString("ID") != null){
		x_ID = rs.getString("ID");
	}else{
		x_ID = "";
	}
	if (rs.getString("PWD") != null){
		x_PWD = rs.getString("PWD");
	}else{
		x_PWD = "";
	}
	if (rs.getString("NICK") != null){
		x_NICK = rs.getString("NICK");
	}else{
		x_NICK = "";
	}
		rs.close();
		rs = null;
	}else if (a.equals("A")) { // Add

		// Get fields from form
		if (request.getParameter("x_ID") != null){
			x_ID = (String) request.getParameter("x_ID");
		}else{
			x_ID = "";
		}
		if (request.getParameter("x_PWD") != null){
			x_PWD = (String) request.getParameter("x_PWD");
		}else{
			x_PWD = "";
		}
		if (request.getParameter("x_NICK") != null){
			x_NICK = (String) request.getParameter("x_NICK");
		}else{
			x_NICK = "";
		}

		// Open record
		String strsql = "SELECT * FROM `account_info` WHERE 0 = 1";
		rs = stmt.executeQuery(strsql);
		rs.moveToInsertRow();

		// Field ID
		tmpfld = ((String) x_ID);
		if (tmpfld == null || tmpfld.trim().length() == 0) {
			tmpfld = "";
		}
		if (tmpfld == null) {
			rs.updateNull("ID");
		}else{
		String srchfld = "'" + tmpfld + "'";
			srchfld = srchfld.replaceAll("'","\\\\'");
			strsql = "SELECT * FROM `account_info` WHERE `ID` = '" + srchfld +"'";
			Statement stmtchk = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
			ResultSet rschk = stmtchk.executeQuery(strsql);
			if (rschk.next()) {
				out.print("Duplicate key for ID, value = " + tmpfld + "<br>");
				out.print("Press [Previous Page] key to continue!");
				return;
			}
			rschk.close();
			rschk = null;
			rs.updateString("ID", tmpfld);
		}

		// Field PWD
		tmpfld = ((String) x_PWD);
		if (tmpfld == null || tmpfld.trim().length() == 0) {
			tmpfld = "";
		}
		if (tmpfld == null) {
			rs.updateNull("PWD");
		}else{
			rs.updateString("PWD", tmpfld);
		}

		// Field NICK
		tmpfld = ((String) x_NICK);
		if (tmpfld == null || tmpfld.trim().length() == 0) {
			tmpfld = "";
		}
		if (tmpfld == null) {
			rs.updateNull("NICK");
		}else{
			rs.updateString("NICK", tmpfld);
		}
		Integer userLevel = (Integer) session.getAttribute("project1_status_UserLevel");
		if (userLevel != null && userLevel.intValue() != -1) { // Non system admin
				rs.updateString("NICK", ((String) session.getAttribute("project1_status_UserID")));
		}
		rs.insertRow();
		rs.close();
		rs = null;
		stmt.close();
		stmt = null;
		conn.close();
		conn = null;
		out.clear();
		response.sendRedirect("account_infolist.jsp");
		response.flushBuffer();
		return;
	}
}catch (SQLException ex){
	out.println(ex.toString());
}
%>
<%@ include file="header.jsp" %>
<div class="content">
	<div class="search">
		<p><span class="jspmaker table_name">账号信息添加<br></span></p>
		<a class="jspmaker back" href="account_infolist.jsp">返回</a>
	</div>
	<script language="JavaScript" src="ew.js"></script>
	<script language="JavaScript">
	<!-- start Javascript
	function  EW_checkMyForm(EW_this) {
	if (EW_this.x_ID && !EW_hasValue(EW_this.x_ID, "TEXT" )) {
	            if (!EW_onError(EW_this, EW_this.x_ID, "TEXT", "Invalid Field - ID"))
	                return false; 
	        }
	if (EW_this.x_PWD && !EW_hasValue(EW_this.x_PWD, "TEXT" )) {
	            if (!EW_onError(EW_this, EW_this.x_PWD, "TEXT", "Invalid Field - PWD"))
	                return false; 
	        }
	if (EW_this.x_NICK && !EW_hasValue(EW_this.x_NICK, "TEXT" )) {
	            if (!EW_onError(EW_this, EW_this.x_NICK, "TEXT", "Invalid Field - NICK"))
	                return false; 
	        }
	return true;
	}
	
	// end JavaScript -->
	</script>
	<form onSubmit="return EW_checkMyForm(this);"  action="account_infoadd.jsp" method="post">
	<p>
	<input type="hidden" name="a" value="A">
	<table border="0" cellspacing="1" cellpadding="4" bgcolor="#CCCCCC">
		<tr>
			<td bgcolor="#594FBF"><span class="jspmaker" style="color: #FFFFFF;">账号</span>&nbsp;</td>
			<td bgcolor="#F5F5F5"><span class="jspmaker"><input type="text" name="x_ID" size="30" maxlength="44" value="<%= HTMLEncode((String)x_ID) %>"></span>&nbsp;</td>
		</tr>
		<tr>
			<td bgcolor="#594FBF"><span class="jspmaker" style="color: #FFFFFF;">密码</span>&nbsp;</td>
			<td bgcolor="#F5F5F5"><span class="jspmaker"><input type="text" name="x_PWD" size="30" maxlength="60" value="<%= HTMLEncode((String)x_PWD) %>"></span>&nbsp;</td>
		</tr>
		<tr>
			<td bgcolor="#594FBF"><span class="jspmaker" style="color: #FFFFFF;">用户名</span>&nbsp;</td>
			<td bgcolor="#F5F5F5"><span class="jspmaker"><% if (session.getAttribute("project1_status_UserLevel") != null && ((Integer) session.getAttribute("project1_status_UserLevel")).intValue() == -1) { // system admin %>
	<input type="text" name="x_NICK" size="30" maxlength="40" value="<%= HTMLEncode((String)x_NICK) %>">
	<%}  else { // not system admin %>
	<% 	x_NICK = ((String) session.getAttribute("project1_status_UserID")); %><% out.print(x_NICK); %><input type="hidden" name="x_NICK" value="<%= HTMLEncode((String)x_NICK) %>">
	<% 	} %>
	</span>&nbsp;</td>
		</tr>
	</table>
	<p>
	<input class="editbutton" type="submit" name="Action" value="提交">
	</form>
</div>
<%@ include file="footer.jsp" %>
